package com.wanmei.controller.product;

import com.wanmei.commons.ResultUtils;
import com.wanmei.pojo.Product;
import com.wanmei.pojo.Productpic;
import com.wanmei.pojo.Result;
import com.wanmei.service.Impl.ProductServiceImpl;
import com.wanmei.service.Impl.ProductpicServiceImpl;
import com.wanmei.util.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("product")
public class DeleteProductController {
    @Autowired
    private ProductServiceImpl productService;
    @Autowired
    private ProductpicServiceImpl productpicService;

    /**
     * 删除商品
     *
     * @param pid
     * @return
     */
    @RequestMapping(value = "deleteProduct", method = RequestMethod.GET)
    public Result deleteProduct(@RequestParam("pid") Integer pid) {
        int result = productService.deleteProductByPid(pid);
        if (result == 0) {
            return ResultUtils.fail("删除失败", null, 0);
        }
        return ResultUtils.success("删除成功", null, 1);
    }

    /**
     * 删除图片
     *
     * @param picid
     * @param pid
     * @return
     */
    @RequestMapping(value = "deleteImg", method = RequestMethod.GET)
    public Result deleteImg(@RequestParam("picid") int picid, @RequestParam("pid") int pid) {
        Product product = productpicService.selectByPID(pid);
        if (product.getProductpicList().size() == 1) {
            return ResultUtils.fail("当前商品图片只有一张无法删除", null, 0);
        }
        int result = productpicService.deleteByPrimaryKey(picid);
        if (result == 0) {
            return ResultUtils.fail("图片删除失败", null, 0);
        }
        return ResultUtils.success("图片删除成功", null, 1);
    }

    /**
     * 上传图片
     *
     * @param pid
     * @return
     */
    @RequestMapping("addImg")
    public Result addImg(@RequestParam("pid") int pid, @RequestParam("imgurl")String imgurl, HttpServletRequest req) {
        Product product = productService.selectByPrimaryKey(pid);
        String realPath = req.getSession().getServletContext().getRealPath("");

            String extensionName = imgurl.substring(imgurl.lastIndexOf(".") + 1);
            String newHeadName = ImageUtil.getImageNewName();
            String fileName = newHeadName + "." + extensionName;
            String path = ImageUtil.REAL_PATH;
            try {
                File file = new File(realPath + imgurl);
                File result = new File(path + fileName);
                InputStream is = new FileInputStream(file);
                OutputStream os = new FileOutputStream(result);
                int in = is.available();
                int len = 0;
                byte[] buffer = new byte[in];
                while ((len = is.read(buffer)) != -1) {
                    os.write(buffer, 0, len);
                }
                os.flush();
                os.close();
                is.close();
                Productpic productpic = new Productpic();
                Date date = new Date();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String nowTime = format.format(date);
                productpic.setAddtime(nowTime);
                System.out.println("");
                productpic.setAdduser("admin");
                productpic.setPicurl(path + fileName);
                productpic.setPid(product.getPid());
                int isSuccess = productpicService.addProductPic(productpic);
                if (isSuccess == 0) {
                    return ResultUtils.fail("添加失败", null, 0);
                }
                return ResultUtils.success("添加成功", null, 1);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        return null;
    }
}